package com.guodefu.demo.config;

import com.guodefu.demo.utils.NumberUtil;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Component
public class DefaultTtlStrategy implements TtlStrategy {
    @Override
    public long getTtl(String ttl) {
        if (NumberUtil.isInteger(ttl)) {
            return Integer.valueOf(ttl);
        }
        if ("today".equalsIgnoreCase(ttl)){
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            // 定义24点（次日凌晨）
            LocalDateTime midnight = LocalDate.now().atStartOfDay();
            // 计算当前时间到24点的秒数
            return Duration.between(now, midnight.plusDays(1)).getSeconds();
        }
        return 5 * 60;
    }
}
